package com.example.newsapp.ui.components.otherComponent.searchComponent

import android.util.Log
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.newsapp.data.compositionLocal.LocalFocusRequest
import com.example.newsapp.data.compositionLocal.LocalNavController
import com.example.newsapp.data.database.NewsItems
import com.example.newsapp.data.`object`.PageNavigation
import com.example.newsapp.data.`object`.StateSave

// 猜你想搜列表
@Composable
fun GuessSearchList(
    guessNumsList: List<NewsItems>,
    modifier: Modifier = Modifier,
) {
    val keyboardController = LocalSoftwareKeyboardController.current
    var rotation = remember { Animatable(0f) }
    var navController = LocalNavController.current
    var focusRequester = LocalFocusRequest.current

    LaunchedEffect(StateSave.searchRefresh) {
        if (StateSave.searchRefresh == true) {
            rotation.animateTo(
                targetValue = 360f,
                animationSpec = tween(durationMillis = 1000)
            )
        }
        rotation.snapTo(0f)
        StateSave.searchRefresh = false
    }
    Column(
        modifier = Modifier
            .pointerInput(Unit) {
                focusRequester.freeFocus()
                keyboardController?.hide()
            }
            .clickable(
                interactionSource = null,
                indication = null
            ) {
                StateSave.hotInitState++
                focusRequester.freeFocus()
                keyboardController?.hide()
            }
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 20.dp, end = 30.dp, top = 10.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = "猜你想搜",
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold
            )
            Icon(
                imageVector = Icons.Default.Refresh,
                contentDescription = "刷新",
                tint = Color(0xFF5B5B5B),
                modifier = Modifier
                    .rotate(rotation.value)
                    .clickable(
                        interactionSource = null,
                        indication = null,
                        onClick = {
                            StateSave.searchRefresh = true
                        }
                    )
            )
        }
        LazyVerticalGrid(
            modifier = Modifier.fillMaxWidth(),
            columns = GridCells.Fixed(2),
            contentPadding = PaddingValues(start = 20.dp, end = 16.dp, bottom = 16.dp, top = 10.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp),
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            itemsIndexed(guessNumsList) { index, item ->
                if (index == 0 || index == 1) {
                    Text(
                        text = item.title,
                        fontSize = 18.sp,
                        color = Color.Red,
                        fontWeight = FontWeight.Medium,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        modifier = Modifier.fillMaxWidth()
                            .clickable {
                                keyboardController?.hide()
                                focusRequester.freeFocus()
                                navController.navigate("${PageNavigation.searchResult}?title=${item.title}")
                            }
                            .pointerInput(Unit) {
                                keyboardController?.hide()
                                focusRequester.freeFocus()
                            }
                    )
                } else {
                    Text(
                        text = item.title,
                        fontSize = 16.sp,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        modifier = Modifier.fillMaxWidth()
                            .clickable {
                                keyboardController?.hide()
                                focusRequester.freeFocus()
                                navController.navigate("${PageNavigation.searchResult}?title=${item.title}")
                            }
                            .pointerInput(Unit) {
                                keyboardController?.hide()
                                focusRequester.freeFocus()
                            }

                    )
                }
            }
        }
    }
}